import axios from 'axios'
import { /** MessageBox, */ Message } from 'element-ui'
import { server } from '../utils/config'

// create an axios instance
const service = axios.create({
  baseURL: server, // url = base url + request url
  withCredentials: false, // send cookies when cross-domain requests
  timeout: 5000 * 4 // request timeout
})

// response interceptor
service.interceptors.response.use(
  response => {
    if (response.status !== 200) {
      Message({
        message: response.message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(new Error(response.message || 'Error'))
    } else {
      return response.data
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.response.data.msg || error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
